package com.quectel.util.common;

import com.quectel.util.ex.CheckErrorException;

import java.security.SecureRandom;

/**
 * 获取随机数
 */
public class RandomUtils {

    private static final int[] NUMS = new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

    private static final char[] CHARS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();

    /**
     * 生成随机数字字符串
     *
     * @param genLength 生成字符串的长度
     * @return
     */
    public static String genDigitalCode(int genLength) {
        int numLen = NUMS.length;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < genLength; i++) {
            sb.append(NUMS[new SecureRandom().nextInt(10)]);
        }
        return sb.toString();
    }

    /**
     * 生成随机字符字符串
     *
     * @param genLength 生成字符串的长度
     * @return
     */
    public static String genCharsCode(int genLength) {
        int charLen = CHARS.length;

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < genLength; i++) {
            sb.append(CHARS[new SecureRandom().nextInt(charLen)]);
        }
        return sb.toString();
    }

    /**
     * 生成随机字符字符串
     *
     * @param minLength 最小长度
     * @param maxLength 最大长度
     * @return
     */
    public static String genCharsCode(int minLength, int maxLength) {
        if (minLength > maxLength) {
            throw new CheckErrorException("最大长度小于最小长度");
        }
        final int x = maxLength - minLength;
        int genLength = new SecureRandom().nextInt(x + 1) + minLength;

        int charLen = CHARS.length;

        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < genLength; i++) {
            sb.append(CHARS[new SecureRandom().nextInt(charLen)]);
        }
        return sb.toString();
    }

    /**
     * 生成强密码 大写+小写+数字
     * 最少六位
     *
     * @param minLen 生成字符串的最小长度
     * @param maxLen 生成字符串的最大长度
     * @return
     */
    public static String genStrongPassword(int minLen, int maxLen) {
        if (minLen < 6) {
            throw new CheckErrorException("最小长度需要大于等于6位");
        }
        int genLength = new SecureRandom().nextInt(maxLen - minLen + 1) + minLen;

        int uppercaseCodesLength = genLength / 3;
        int lowercaseCodesLength = genLength / 4;
        int digitalCodesLength = genLength - lowercaseCodesLength - uppercaseCodesLength;

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < uppercaseCodesLength; i++) {
            //大写
            sb.append((char) (new SecureRandom().nextInt(26) + 65));
        }
        for (int i = 0; i < lowercaseCodesLength; i++) {
            //小写
            sb.append((char) (new SecureRandom().nextInt(26) + 97));
        }
        for (int i = 0; i < digitalCodesLength; i++) {
            //数字
            sb.append(new SecureRandom().nextInt(10));
        }

        return sb.toString();
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            System.out.println(genCharsCode(4));
        }
    }
}
